package edu.ubbcluj.turisztikai.activity;

//import java.sql.Connection;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import turistic.GUI.graphic.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import edu.ubbcluj.turisztikai.connection.Connection;
import edu.ubbcluj.turisztikai.connection.Hash2;

public class RegActivity extends Activity {
	Connection conn;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registration);
		Button okB = (Button) findViewById(R.id.regB);
		okB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText user = (EditText) findViewById(R.id.loginEmail);
				String userStr = user.getText().toString();
				
				EditText pass = (EditText) findViewById(R.id.loginPassword);
				String passStr = pass.getText().toString();

				EditText email = (EditText) findViewById(R.id.editText3);
				String emailStr = email.getText().toString();
				messageBox(userStr+" "+passStr);
				try {
				    //Generate a temporary key. In practice, you would save this key.
				    // See also Encrypting with DES Using a Pass Phrase.
				   SecretKey key = KeyGenerator.getInstance("DES").generateKey();
					 // Create encrypter/decrypter class
				   Hash2 encrypter = new Hash2(key);
				    // Encrypt
				   String encrypted = encrypter.encrypt(passStr);
				    //String mess = conn.getMess();
				    
				    encrypted = encrypted.replace("\n","");
				    //messageBox(encrypted);
				    
				     conn = new Connection("|"+"rgflg"+"|"+userStr+"|"+encrypted+"|"+emailStr);
				    // conn = new Connection("|"+"rgflg"+"|");
				} catch (Exception e) {
				}
				String mess = conn.getMess();
			
				if (mess.equals("A regisztracio sikeres!")){
					messageBox(mess);
					startActivity(new Intent(RegActivity.this,
						TurisztikaiUtvonalTervezoActivity.class));
				
				}
				else messageBox(mess);

			}
		});
			
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(RegActivity.this,
					TurisztikaiUtvonalTervezoActivity.class));
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void messageBox(String mess) {
		final AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();

		alertDialog.setTitle("Uzenet");
		alertDialog.setMessage(mess);

		alertDialog.show();

	}

}
